package Jun13.query;

import Jun13.utils.ESUtils;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

public class SearchAggsDoc {

    public static void main(String[] args) throws IOException {
        RestHighLevelClient rest = ESUtils.getRest();//获取ES对象
        SearchRequest search = new SearchRequest().indices("user");//创建查询对象
        SearchSourceBuilder scb = new SearchSourceBuilder();//创建查询条件构建对象
        search.source(scb.aggregation(AggregationBuilders.stats("test").field("age")));
        SearchResponse result = rest.search(search, RequestOptions.DEFAULT);//发送包含有查询条件构造对象的查询对象
        SearchHit[] hits = result.getHits().getHits();//获取查询结果
        for (SearchHit s:hits){
            System.out.println(s);//遍历并输出结果
        }
        rest.close();//关闭
    }
}
